//
//  GoodListRightTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "GoodListRightTableViewCell.h"

@interface GoodListRightTableViewCell ()

Strong UILabel *titleLabel;
Strong UIImageView *titleImageView;
Strong UIButton *addButton; /**<*/
Strong UIButton *deleteButton; /**<*/
Strong UILabel *numberLabel; /**<*/

@end

@implementation GoodListRightTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleImageView = [UIImageView new];
        _titleImageView.backgroundColor = getColor(@"333333");
        _titleImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 0;
        
        _addButton = [[UIButton alloc]init];
        [_addButton setImage:[UIImage imageNamed:@"MakeAnAppointment_add"] forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _deleteButton = [[UIButton alloc]init];
        [_deleteButton setImage:[UIImage imageNamed:@"MakeAnAppointment_delete"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _numberLabel  = [[UILabel alloc]init];
        _numberLabel.font = DEF_FontSize_14;
        _numberLabel.textColor = getColor(textColor);
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        _numberLabel.adjustsFontSizeToFitWidth = YES;
        
        [self.contentView sd_addSubviews:@[_titleLabel , _titleImageView , _addButton , _deleteButton , _numberLabel]];
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(-10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(30 / WIDTH_5S_SCALE);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 5 / WIDTH_5S_SCALE);
        make.top.mas_equalTo((58 / WIDTH_5S_SCALE - 20 / WIDTH_5S_SCALE) / 2);
        make.height.mas_equalTo(20 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(20 / WIDTH_5S_SCALE);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.addButton.mas_left).offset(0);
        make.width.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.numberLabel.mas_left).offset(0);
        make.top.mas_equalTo((58 / WIDTH_5S_SCALE - 20 / WIDTH_5S_SCALE) / 2);
        make.height.mas_equalTo(20 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(20 / WIDTH_5S_SCALE);
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    [self.titleImageView sd_setImageWithURL:[NSURL URLOffsetIPWithString:model[@"first_picture"]]];
    if ([self.number isEqualToString:@"0"]) {
        self.numberLabel.hidden = YES;
        self.deleteButton.hidden = YES;
    }else{
        self.numberLabel.hidden = NO;
        self.deleteButton.hidden = NO;
    }
    self.numberLabel.text = self.number;
   
    self.titleLabel.attributedText = [self getAttributeName:model[@"c_name"]  one:model[@"unit_price"] second:model[@"c_praise"] three:model[@"c_popularity"] four:model[@"c_distribution"]];
}

- (NSAttributedString *)getAttributeName:(NSString *)name one:(NSString *)one second:(NSString *)second three:(NSString *)three four:(NSString *)four{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];

    NSAttributedString *oneStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"    ¥%@",one] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *secondAtt = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"+%@",second] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *threeStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"+%@",three] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *fourAtt = [[NSAttributedString alloc]initWithString:four attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:oneStr];
 
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"好评"]];
    [attStr appendAttributedString:secondAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"/ "]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"人气"]];
    [attStr appendAttributedString:threeStr];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"是否配送:"]];
    [attStr appendAttributedString:fourAtt];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}
- (void)addButtonClick{
    if (_getAddNumberClick) {
        _getAddNumberClick();
    }
}

- (void)deleteButtonClick{
    if (_getDeleteNumberClick) {
        _getDeleteNumberClick();
    }
}

@end
